package com.ttn.business.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ttn.business.service.CourseUnitOralConversationService;
import com.ttn.common.core.entity.PageQuery;
import com.ttn.common.core.entity.Result;
import com.ttn.common.core.entity.business.CourseUnitOralConversation;
import com.ttn.common.core.entity.business.bo.ModifyCourseUnitOralConversationSortBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * (course_unit_oral_conversation)表控制层
 *
 * @author xxxxx
 */
@RestController
@RequestMapping("/course-unit-oral-conversation")
@Slf4j
public class CourseUnitOralConversationController {
    /**
     * 服务对象
     */
    @Resource
    private CourseUnitOralConversationService courseUnitOralConversationService;

    /**
     * 保存口语会话
     *
     * @param request
     * @param courseUnitOralConversation
     * @return
     */
    @PostMapping("/save/course-unit-oral-conversation")
    public Result saveCourseUnitOralConversation(HttpServletRequest request, @RequestBody CourseUnitOralConversation courseUnitOralConversation) {
        log.info("CourseUnitOralConversationController saveCourseUnitOralConversation ,参数:{}", courseUnitOralConversation);
        LambdaQueryWrapper<CourseUnitOralConversation> courseUnitOralConversationLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitOralConversationLambdaQueryWrapper.eq(CourseUnitOralConversation::getCourseUnitId, courseUnitOralConversation.getCourseUnitId());
        courseUnitOralConversation.setSort(Math.toIntExact(courseUnitOralConversationService.count(courseUnitOralConversationLambdaQueryWrapper)) + 1);
        courseUnitOralConversationService.save(courseUnitOralConversation);
        return Result.success();
    }

    /**
     * 修改口语会话
     *
     * @param request
     * @param courseUnitOralConversation
     * @return
     */

    @PutMapping("/modify/course-unit-oral-conversation")
    public Result modifyCourseUnitOralConversation(HttpServletRequest request, @RequestBody CourseUnitOralConversation courseUnitOralConversation) {
        log.info("CourseUnitOralConversationController saveCourseUnitOralPractice ,参数:{}", courseUnitOralConversation);

        courseUnitOralConversationService.updateById(courseUnitOralConversation);
        return Result.success();
    }

    /**
     * 修改口语会话排序
     *
     * @param request
     * @param modifyCourseUnitOralConversationSortBO
     * @return
     */
    @PutMapping("/modify/course-unit-oral-conversation-sort")
    public Result modifyCourseUnitOralConversationSort(HttpServletRequest request, @RequestBody ModifyCourseUnitOralConversationSortBO modifyCourseUnitOralConversationSortBO) {
        log.info("CourseUnitOralConversationController modifyCourseUnitOralConversationSort ,参数:{}", modifyCourseUnitOralConversationSortBO);
        CourseUnitOralConversation courseUnitOralConversation = courseUnitOralConversationService.getById(modifyCourseUnitOralConversationSortBO.getId());
        LambdaQueryWrapper<CourseUnitOralConversation> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.ne(CourseUnitOralConversation::getId, modifyCourseUnitOralConversationSortBO.getId());
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitOralConversation::getCourseUnitId,
                courseUnitOralConversation.getCourseUnitId());
        courseUnitGrammarTestLambdaQueryWrapper.orderByAsc(CourseUnitOralConversation::getSort);
        List<CourseUnitOralConversation> courseUnitOralConversationList = courseUnitOralConversationService.list(courseUnitGrammarTestLambdaQueryWrapper);
        if (courseUnitOralConversationList.size() > 0) {
            int sortIndex = 0;
            if (modifyCourseUnitOralConversationSortBO.getSortingBehavior().equals(-1)) {
                if (courseUnitOralConversation.getSort() - 2 > 0) {
                    sortIndex = courseUnitOralConversation.getSort() - 2;
                }
            } else if (modifyCourseUnitOralConversationSortBO.getSortingBehavior().equals(1)) {
                sortIndex = courseUnitOralConversation.getSort();
            }
            courseUnitOralConversationList.add(sortIndex, courseUnitOralConversation);
            AtomicInteger i = new AtomicInteger(1);
            courseUnitOralConversationList.stream().forEach(courseUnitGrammarTest1 -> {
                courseUnitGrammarTest1.setSort(i.getAndIncrement());
            });
            courseUnitOralConversationService.updateBatchById(courseUnitOralConversationList);
        }
        return Result.success();
    }

    /**
     * 删除口语会话
     *
     * @param request
     * @param id
     * @return
     */

    @DeleteMapping("/delete/course-unit-oral-conversation-by-id/{id}")
    public Result deleteCourseUnitOralConversationById(HttpServletRequest request, @PathVariable("id") Integer id) {
        log.info("CourseUnitOralConversationController deleteCourseUnitOralConversationById ,参数id:{}", id);
        CourseUnitOralConversation courseUnitOralConversation = courseUnitOralConversationService.getById(id);
        courseUnitOralConversationService.removeById(id);
        LambdaQueryWrapper<CourseUnitOralConversation> courseUnitGrammarTestLambdaQueryWrapper = new LambdaQueryWrapper();
        courseUnitGrammarTestLambdaQueryWrapper.ne(CourseUnitOralConversation::getId, id);
        courseUnitGrammarTestLambdaQueryWrapper.eq(CourseUnitOralConversation::getCourseUnitId, courseUnitOralConversation.getCourseUnitId());
        courseUnitGrammarTestLambdaQueryWrapper.orderByAsc(CourseUnitOralConversation::getSort);
        List<CourseUnitOralConversation> courseUnitOralConversationList = courseUnitOralConversationService.list(courseUnitGrammarTestLambdaQueryWrapper);
        if (courseUnitOralConversationList.size() > 0) {
            AtomicInteger i = new AtomicInteger(1);
            courseUnitOralConversationList.stream().forEach(courseUnitOralConversation1 -> {
                courseUnitOralConversation1.setSort(i.getAndIncrement());
            });
            courseUnitOralConversationService.updateBatchById(courseUnitOralConversationList);
        }
        return Result.success();
    }

    /**
     * 通过单元id 查询口语会话
     *
     * @param request
     * @param courseUnitId
     * @param pageQuery
     * @return
     */
    @PostMapping("/query/course-unit-oral-conversation-by-course-unit-id-page/{courseUnitId}")
    public Result queryCourseUnitOralConversationByCourseUnitIdPage(HttpServletRequest request, @PathVariable("courseUnitId") Integer courseUnitId, @RequestBody PageQuery pageQuery) {
        log.info("CourseUnitOralConversationController queryCourseUnitOralConversationByCourseUnitIdPage ,参数pageQuery:{},courseUnitId:{}", pageQuery, courseUnitId);
        IPage<CourseUnitOralConversation> courseUnitOralPracticeIPage = new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize());
        LambdaQueryWrapper<CourseUnitOralConversation> courseUnitLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitLambdaQueryWrapper.eq(CourseUnitOralConversation::getCourseUnitId, courseUnitId);
        courseUnitLambdaQueryWrapper.orderByAsc(CourseUnitOralConversation::getSort);
        return Result.success(courseUnitOralConversationService.page(courseUnitOralPracticeIPage, courseUnitLambdaQueryWrapper));
    }

    @PostMapping("/service/query/course-unit-oral-conversation-by-course-unit-id/{courseUnitId}")
    public Result serviceQueryCourseUnitOralConversationByCourseUnitId(@PathVariable(
            "courseUnitId") Integer courseUnitId) {
        log.info("CourseUnitOralConversationController serviceQueryCourseUnitOralConversationByCourseUnitId ,参数courseUnitId:{}",
                courseUnitId);
        LambdaQueryWrapper<CourseUnitOralConversation> courseUnitOralConversationLambdaQueryWrapper = new LambdaQueryWrapper<>();
        courseUnitOralConversationLambdaQueryWrapper.eq(CourseUnitOralConversation::getCourseUnitId, courseUnitId);
        return Result.success(courseUnitOralConversationService.count(courseUnitOralConversationLambdaQueryWrapper));
    }

}
